import hashlib
import random
import time
import requests,json

def Password_hash(data):
    zhuid = data
    hash=hashlib.md5()
    hash.update((zhuid).encode('utf-8'))
    sig = hash.hexdigest()
    return sig.upper()

def AccessKey(AccountId,PhoneNos,Password,Random,Timestamp):
    zhuid = "AccountId={}&PhoneNos={}&Password={}&Random={}&Timestamp={}".format(AccountId,PhoneNos,Password,Random,Timestamp)
    hash=hashlib.sha256()
    hash.update((zhuid).encode())
    sig = hash.hexdigest()
    return sig

def random_num():
    return random.randint(1,10000000) 

def time_trun():
    return int(time.time())


def send_messages(AccountId,AccessKey,Timestamp,Random,ProductId,PhoneNos,Content):
    url='http://api.51welink.com/EncryptionSubmit/SendSms.ashx'
    data = {
	'AccountId':AccountId,
	'AccessKey':AccessKey,
	'Timestamp':Timestamp,
	'Random':Random,
	'ExtendNo': "",
	'ProductId': ProductId,
	'PhoneNos': PhoneNos,
	'Content':"{}【微网通联】".format(Content),
     }
    print(data)
    r =requests.post(url,data)
    return r.text


#print(AccessKey('yanfa001','13699999999','B54B89712EB997BE99114478E3673E3F',6203922,1532928860))

def run(phone,content):
    AccountId='dlchenyingzhi'
    password= Password_hash('dlchenyingzhi123SMmsEncryptKey')
    print(password)
    Random= random_num()
    timetamp= time_trun()
    Key=AccessKey('dlchenyingzhi',phone,password,Random,timetamp)
    data = send_messages(AccountId,Key,timetamp,Random,1012818,phone,content)
    return data

#print(run('18878774260','北方激光'))
